package poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteXls {
	public static void main(String[] args) throws IOException {
		office2007();
		
	}
	public static void office2003() throws IOException{
		File xlsx= new File("E:/123456.xls");
		HSSFWorkbook wb= new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("new sheet");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue(1);
		row.createCell(1).setCellValue(1.1);
		row.createCell(2).setCellValue("test");
		row.createCell(3).setCellValue(true);
        HSSFCellStyle cellStyle = wb.createCellStyle();		
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		HSSFCell dCell = row.createCell(4);
		dCell.setCellValue(new Date());
		dCell.setCellStyle(cellStyle);
		HSSFCell csCell =row.createCell(5);
		csCell.setCellValue("中文测试_Chinese Words Test");
		FileOutputStream fileOut= new FileOutputStream(xlsx);
		wb.write(fileOut);
		fileOut.close();
	}
	
	
	public static void office2007() throws IOException{
		File xlsx= new File("E:/123456.xlsx");
		XSSFWorkbook wb= new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("new sheet");
		XSSFRow row = sheet.createRow(0);
		XSSFCell cell = row.createCell(0);
		cell.setCellValue(1);
		row.createCell(1).setCellValue(1.1);
		row.createCell(2).setCellValue("test");
		row.createCell(3).setCellValue(true);
        XSSFCellStyle cellStyle = wb.createCellStyle();		
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER_SELECTION);
		XSSFCell dCell = row.createCell(4);
		dCell.setCellValue(new Date());
		dCell.setCellStyle(cellStyle);
		XSSFCell csCell =row.createCell(5);
		csCell.setCellValue("中文测试_Chinese Words Test");
		FileOutputStream fileOut= new FileOutputStream(xlsx);
		wb.write(fileOut);
		fileOut.close();
		String cmd = "cmd /c start E:/123456.xlsx";
		Runtime.getRuntime().exec(cmd);
	}
}
